package com.mt.fpb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.BankCardMapper;
import com.mt.fpb.mapper.BankInfoMapper;
import com.mt.fpb.mapper.RxEmployeeMapper;
import com.mt.fpb.model.BankCard;
import com.mt.fpb.model.BankInfo;
import com.mt.fpb.model.RxEmployee;
import com.mt.fpb.model.dto.BindCardDto;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 绑定银行卡控制器
 * @author yu
 * @date 2021年08月14日 17:41
 */
@RestController
@RequestMapping("bank")
public class BankBindingController {

    @Autowired
    private RxEmployeeMapper rxEmployeeMapper;

    @Autowired
    private BankCardMapper bankCardMapper;

    @Autowired
    private BankInfoMapper bankInfoMapper;

    /**
     * 银行卡绑定接口
     * @Author yu6
     * @date 2021/8/14 17:51
     * @Param [name, idCard, bankName, bankCardId, phoneNumber]
     * @return com.mt.fpb.model.vo.CommonResult
     **/
    @PostMapping("bankBinding")
    public CommonResult binding(String name,String idCard,String bankName,String bankCardId,String phoneNumber){

        //通过姓名和身份证号查询员工表的员工id
        QueryWrapper<RxEmployee> query = new QueryWrapper<RxEmployee>();
        query.eq("name",name);
        query.eq("id_card",idCard);
        RxEmployee employee = rxEmployeeMapper.selectOne(query);
        int employeeId =  employee.getEmployeeId();
        System.out.println("employeeId:"+employeeId);

        //创建bankCard实体类
        BankCard bankCard = new BankCard();
        bankCard.setBankCardId(bankCardId);
        bankCard.setBankId("1122");
        bankCard.setIdCard(idCard);
        bankCard.setBankCardName(bankName);
        bankCard.setEmployeeId(employeeId);
        bankCard.setCardStu(1);

        //添加新的银行卡时修改其他银行卡的默认状态
        int d = bankCardMapper.updateStatus(1);
        System.out.println("d = " + d);
        //向bank_card中插入数据
        int result = bankCardMapper.insert(bankCard);
        if(result>0){

            return CommonResult.success(ResponseCode.SUCCESS);
        }else{
            return CommonResult.success(ResponseCode.FAILED);
        }


    }

    /**
     * 获取当前用户添加的银行卡列表
     * @Author yu
     * @date 2021/8/16 17:03
     * @Param [employeeId]
     * @return com.mt.fpb.model.vo.CommonResult
     **/
    @GetMapping("getBindCard")
    public CommonResult getBindCard(String employeeId){

        List<BindCardDto> cardList = new ArrayList();

        QueryWrapper<BankCard> query = new QueryWrapper<>();
        query.eq("employee_id",employeeId);
        List<BankCard> list = bankCardMapper.selectList(query);
        for(BankCard bank : list){
            String bankCardName =  bank.getBankCardName();
            String bankCardId = bank.getBankCardId();
            BindCardDto bindCardDto = new BindCardDto();
            bindCardDto.setBankName(bankCardName);
            bindCardDto.setBankCardId(bankCardId);
            cardList.add(bindCardDto);
        }

        return CommonResult.success(cardList);
    }





    /**
     * 获取所有支持的银行
     * @Author yu
     * @date 2021/8/16 10:00
     * @Param []
     * @return com.mt.fpb.model.vo.CommonResult
     **/

    @GetMapping("getAllBank")
    public CommonResult getAllBankInfo(){

        List<BankInfo> bankList = bankInfoMapper.selectList(null);
        return CommonResult.success(bankList);
    }

}
